home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / DefaultComboBoxModel.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.2 KB  |  170 lines  |  [TEXT/CWIE]

  1. /**
  2.  * @(#)DefaultComboBoxModel.java    1.5 98/08/26
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.beans.*;
  17. import java.util.*;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21.  
  22. import java.io.Serializable;
  23. import java.io.ObjectOutputStream;
  24. import java.io.ObjectInputStream;
  25. import java.io.IOException;
  26.  
  27. import javax.swing.event.*;
  28. import javax.swing.plaf.*;
  29. import javax.swing.border.*;
  30.  
  31. import javax.accessibility.*;
  32.  
  33. /**
  34.  * The default model for combo boxes.
  35.  *
  36.  * @version 1.5 08/26/98
  37.  * @author Arnaud Weber
  38.  * @author Tom Santos
  39.  */
  40.  
  41. public class DefaultComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
  42.     Vector objects;
  43.     Object selectedObject;
  44.  
  45.     /**
  46.      * Constructs an empty DefaultComboBoxModel object.
  47.      */
  48.     public DefaultComboBoxModel() {
  49.         objects = new Vector();
  50.     }
  51.  
  52.     /**
  53.      * Constructs a DefaultComboBoxModel object initialized with
  54.      * an array of objects.
  55.      *
  56.      * @param items  an array of Object objects
  57.      */
  58.     public DefaultComboBoxModel(final Object items[]) {
  59.         objects = new Vector();
  60.     objects.ensureCapacity( items.length );
  61.  
  62.         int i,c;
  63.     for ( i=0,c=items.length;i<c;i++ )
  64.         objects.addElement(items[i]);
  65.  
  66.     if ( getSize() > 0 ) {
  67.         selectedObject = getElementAt( 0 );
  68.     }
  69.     }
  70.  
  71.     /**
  72.      * Constructs a DefaultComboBoxModel object initialized with
  73.      * a vector.
  74.      *
  75.      * @param v  a Vector object ...
  76.      */
  77.     public DefaultComboBoxModel(Vector v) {
  78.     objects = v;
  79.  
  80.     if ( getSize() > 0 ) {
  81.         selectedObject = getElementAt( 0 );
  82.     }
  83.     }
  84.  
  85.     // implements javax.swing.ComboBoxModel
  86.     public void setSelectedItem(Object anObject) {
  87.         selectedObject = anObject;
  88.     fireContentsChanged(this, -1, -1);
  89.     }
  90.  
  91.     // implements javax.swing.ComboBoxModel
  92.     public Object getSelectedItem() {
  93.         return selectedObject;
  94.     }
  95.  
  96.     // implements javax.swing.ListModel
  97.     public int getSize() {
  98.         return objects.size();
  99.     }
  100.  
  101.     // implements javax.swing.ListModel
  102.     public Object getElementAt(int index) {
  103.         if ( index >= 0 && index < objects.size() )
  104.         return objects.elementAt(index);
  105.     else
  106.         return null;
  107.     }
  108.  
  109.     /**
  110.      * Returns the index-position of the specified object in the list.
  111.      *
  112.      * @param anObject  
  113.      * @return an int representing the index position, where 0 is 
  114.      *         the first position
  115.      */
  116.     public int getIndexOf(Object anObject) {
  117.         return objects.indexOf(anObject);
  118.     }
  119.  
  120.     // implements javax.swing.MutableComboBoxModel
  121.     public void addElement(Object anObject) {
  122.         objects.addElement(anObject);
  123.     fireIntervalAdded(this,objects.size()-1, objects.size()-1);
  124.         if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
  125.         setSelectedItem( anObject );
  126.     }
  127.     }
  128.  
  129.     // implements javax.swing.MutableComboBoxModel
  130.     public void insertElementAt(Object anObject,int index) {
  131.         objects.insertElementAt(anObject,index);
  132.     fireIntervalAdded(this, index, index);
  133.     }
  134.  
  135.     // implements javax.swing.MutableComboBoxModel
  136.     public void removeElementAt(int index) {
  137.         if ( getElementAt( index ) == selectedObject ) {
  138.         if ( index == 0 ) {
  139.             setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
  140.         }
  141.         else {
  142.             setSelectedItem( getElementAt( index - 1 ) );
  143.         }
  144.     }
  145.  
  146.     objects.removeElementAt(index);
  147.  
  148.     fireIntervalRemoved(this, index, index);
  149.     }
  150.  
  151.     // implements javax.swing.MutableComboBoxModel
  152.     public void removeElement(Object anObject) {
  153.         int index = objects.indexOf(anObject);
  154.     if ( index != -1 ) {
  155.         removeElementAt(index);
  156.     }
  157.     }
  158.  
  159.     /**
  160.      * Empties the list.
  161.      */
  162.     public void removeAllElements() {
  163.         int firstIndex = 0;
  164.     int lastIndex = objects.size()-1;
  165.     objects.removeAllElements();
  166.     selectedObject = null;
  167.     fireIntervalRemoved(this, firstIndex, lastIndex);
  168.     }
  169. }
  170.